// releasePage/pages/operatorInfo/operatorInter/operatorInter.js
import Http from '../../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        ipxBottom: 0,
        companyContent: [],
        operatorArr: [],
        operatorIndex: 0,
        operatorId: '',
        typeIndex: Number,
        typeArr: ['2G', '3G', '4G', '5G', '无信号'],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _that = this;
        if (app.globalData.isIpx) {
            _that.setData({
                ipxBottom: 34,
            });
        }
        if (options.operatorId) {
            for (let i = 0; i < _that.data.typeArr.length; i++) {
                if (options.typeValue == _that.data.typeArr[i]) {
                    _that.setData({
                        typeIndex: i,
                    });
                }
            }
            _that.setData({
                operatorId: options.operatorId,
            });
        }
        console.log(options.operatorId);
        console.log(options.typeValue);
        _that.getOperatorList(options.operatorId);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    operatorSelect(e) {
        console.log(e.currentTarget.dataset.index);
        this.setData({
            operatorIndex: e.currentTarget.dataset.index,
            operatorId: e.currentTarget.dataset.id,
        });
        console.log(this.data.operatorIndex);
    },
    typeSelect(e) {
        console.log(e.currentTarget.dataset.index);
        this.setData({
            typeIndex: e.currentTarget.dataset.index,
        });
        console.log(this.data.typeIndex);
    },
    backTap(e) {
        let _that = this;
        let index = 0;
        if (_that.data.typeArr[_that.data.typeIndex]) {
            if (_that.data.typeIndex == 4) {
                index = 6; //无卡
            } else if (_that.data.typeIndex == 3) {
                index = 0; //5G
            } else if (_that.data.typeIndex == 2) {
                index = 1; //4G
            } else if (_that.data.typeIndex == 1) {
                if (
                    _that.data.companyContent[
                        _that.data.operatorIndex
                    ].networksupport.includes('08')
                ) {
                    index = 3; //3G CDMA2000
                } else {
                    index = 2; //其他
                }
            } else if (_that.data.typeIndex == 0) {
                if (
                    _that.data.companyContent[
                        _that.data.operatorIndex
                    ].networksupport.includes('11')
                ) {
                    index = 5; //2G CDMA1X
                } else {
                    index = 4; //2G GSM
                }
            }
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2];
            prevPage.setData({
                operatorType: _that.data.operatorArr[_that.data.operatorIndex],
                operatorindex: index,
                operatorId: _that.data.operatorId,
                editOperatorValue: [],
                operator: {},
                networktype: _that.data.typeArr[_that.data.typeIndex],
            });
            prevPage.operatorContent(index);
            wx.navigateBack({
                delta: 1, // 返回上一级页面。
            });
        } else {
            wx.showToast({
                title: '请将信息填写完整',
                icon: 'none',
                duration: 1500,
            });
        }
    },
    // 获取运营商列表
    getOperatorList(id) {
        let _that = this;
        let country;
        if (app.globalData.countryBack == '中国') {
            if (app.globalData.addressBack == '香港') {
                country = '中国香港';
            } else if (
                app.globalData.taiwan.includes(app.globalData.addressBack)
            ) {
                country = '中国台湾';
            } else if (app.globalData.addressBack == '澳门') {
                country = '中国澳门';
            } else {
                country = '中国大陆';
            }
        } else {
            country = app.globalData.countryBack;
        }
        let data = {
            authId: app.globalData.authId,
            pageSize: 100,
            pageNo: 1,
            country: country,
        };
        http.getRequest(`cjMap/getCityOperatorList`, data).then((res) => {
            let operatorid = res.data[0].id;
            for (let i = 0; i < res.data.length; i++) {
                res.data[i].checked = false;
                _that.data.operatorArr.push(res.data[i].operator);
                if (id == res.data[i].id) {
                    operatorid = res.data[i].id;
                    _that.setData({
                        operatorIndex: i,
                    });
                }
            }
            console.log(_that.data.operatorIndex);
            _that.setData({
                companyContent: res.data,
                operatorArr: _that.data.operatorArr,
                operatorId: operatorid,
            });
        });
    },
});
